package com.jgp.glmeeting.mobile.controller;


import com.alibaba.fastjson.JSON;
import com.jgp.glmeeting.mobile.commons.BaseResponse;
import com.jgp.glmeeting.mobile.commons.ErrorMsg;
import com.jgp.glmeeting.mobile.dto.InvoiceBean;
import com.jgp.glmeeting.model.Arrangement;
import com.jgp.glmeeting.model.Invoice;
import com.jgp.glmeeting.model.Participants;
import com.jgp.glmeeting.service.ArrangementService;
import com.jgp.glmeeting.service.InvoiceService;
import com.jgp.glmeeting.service.ParticipantsService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * 房费结算
 */
@RestController
@RequestMapping("mobile/MobileInvoiceController")
public class MobileInvoiceController {

    private final Logger logger = LoggerFactory.getLogger(MobileInvoiceController.class);

    @Autowired
    private ParticipantsService participantsService;
    @Autowired
    private ArrangementService arrangementService;
    @Autowired
    private InvoiceService invoiceService;


    /**
     * 查询需要结算的人员信息
     * @param group
     * @return
     */
    @RequestMapping("queryInvoiceInfo")
    public BaseResponse queryInvoiceInfo(String group, String meetingId) {
        if(StringUtils.isBlank(group) || StringUtils.isBlank(meetingId)){
            return new BaseResponse(ErrorMsg.INVALID_PARAM.getCode(), ErrorMsg.INVALID_PARAM.getErrorMsg());
        }
        try {
            BaseResponse baseResponse = new BaseResponse(200);
            List<Participants> participantsList = participantsService.queryListByGroupAndId(null,group);
            List<InvoiceBean> invoiceList = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(participantsList)){
                for (Participants p: participantsList) {
                    List<Arrangement> arrangements = arrangementService.queryListByParticipantsId(p.getId());
                    if(CollectionUtils.isNotEmpty(arrangements)){
                        InvoiceBean invoice = new InvoiceBean();
                        invoice.setHotel(arrangements.get(0).getRoomNo());
                        invoice.setMeetingId(meetingId);
                        invoice.setParticipantsId(p.getId());
                        invoice.setParticipantName(p.getName());
                        invoice.setInvoiceHead(p.getInvoiceHead());
                        invoice.setMobilePhone(p.getMobilePhone());
                        invoice.setOrganizationCode(p.getOrganizationCode());
                        invoice.setWorkUnit(p.getWorkUnit());
                        StringBuilder builder = new StringBuilder();
                        for (Arrangement a: arrangements) {
                            LocalDate date = a.getPlanDate();
                            builder.append(date.getYear() + "年" + date.getMonthValue() + "月" + date.getDayOfMonth()).append(",");
                        }
                        String checkInTime = builder.toString();
                        invoice.setCheckInTime(checkInTime.substring(0, checkInTime.length()-1));
                        invoice.setIsInvoiced(p.getIsInvoiced());
                        invoiceList.add(invoice);
                    }
                }
            }
            baseResponse.setObj(invoiceList);
            return baseResponse;
        }catch (Exception e) {
            logger.error("查询需要结算的人员信息异常, group={},meetingId={}",group, meetingId,e);
            return new BaseResponse(ErrorMsg.SERVER_ERROR.getCode(), ErrorMsg.SERVER_ERROR.getErrorMsg());
        }
    }


    /**
     * 保存发票
     * @param invocesStr
     * @return
     */
    @RequestMapping("saveInvoices")
    public BaseResponse saveInvoices(String invocesStr) {
        if(StringUtils.isBlank(invocesStr)){
            return new BaseResponse(ErrorMsg.INVALID_PARAM.getCode(), ErrorMsg.INVALID_PARAM.getErrorMsg());
        }
        try {
            List<Invoice> invoiceList = JSON.parseArray(invocesStr, Invoice.class);
            if(CollectionUtils.isNotEmpty(invoiceList)) {
                invoiceService.insertList(invoiceList);
            }
            BaseResponse baseResponse = new BaseResponse(200);
            return baseResponse;
        }catch (Exception e) {
            logger.error("查询需要结算的人员信息异常, invocesStr={}",invocesStr,e);
            return new BaseResponse(ErrorMsg.SERVER_ERROR.getCode(), ErrorMsg.SERVER_ERROR.getErrorMsg());
        }
    }
}
